<?php

/**
 * comment actions.
 *
 * @package    myarticles
 * @subpackage comment
 * @author     Apul Gupta<apulmca2k4@gmail.com>
 */
class commentActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
        $this->comments = commentPeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {    
        $this->form = new commentForm(); 
  }

  public function executeCreate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST));

      $this->form = new commentForm();

      $form = $this->processForm($request, $this->form);

      $requested = $request->getParameter($this->form->getName());
      $article = ArticlePeer::retrieveByPK($requested['article_id']);
      if(!$form->isValid())
      {
        $this->getUser()->setAttribute('form', $form);
        $this->getUser()->setFlash('message','Please fill necessary details carefully.');
      }
      else
      {
          $this->getUser()->setFlash('message','Your Comment is pending for moderation.');
      }
      $this->redirect($this->generateUrl('article', $article));
    //$this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($comment = commentPeer::retrieveByPk($request->getParameter('id')), sprintf('Object comment does not exist (%s).', $request->getParameter('id')));
    $this->form = new commentForm($comment);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($comment = commentPeer::retrieveByPk($request->getParameter('id')), sprintf('Object comment does not exist (%s).', $request->getParameter('id')));
    $this->form = new commentForm($comment);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($comment = commentPeer::retrieveByPk($request->getParameter('id')), sprintf('Object comment does not exist (%s).', $request->getParameter('id')));
    $comment->delete();

    $this->redirect('comment/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $comment = $form->save();
      $this->getUser()->getAttributeHolder()->clear();      
    }
    return $form;
  }

  public function executeVoteUp(sfWebRequest $request)
  {
      $this->forward404Unless($request->isXmlHttpRequest());
      $comment = CommentPeer::retrieveByPK($request->getParameter('id'));
     
      $comment->setTotalUsers($comment->getTotalUsers()+1);
      $vote = $comment->getTotalVotes()+1;
      $comment->setTotalVotes($vote);
      $comment->save();
      
      $this->getResponse()->setContent($vote);
      $this->setLayout(false);
      return sfView::NONE;
  }
  public function executeVoteDown(sfWebRequest $request)
  {
      $this->forward404Unless($request->isXmlHttpRequest());
      $comment = CommentPeer::retrieveByPK($request->getParameter('id'));

      $comment->setTotalUsers($comment->getTotalUsers()+1);
      $vote = $comment->getTotalVotes()-1;
      $comment->setTotalVotes($vote);
      $comment->save();

      $this->getResponse()->setContent($vote);
      $this->setLayout(false);
      return sfView::NONE;
  }

  public function executeAbuseComment(sfWebRequest $request)
  {
    $this->forward404Unless($request->isXmlHttpRequest());
    $comment = CommentPeer::retrieveByPK($request->getParameter('id'));

    $comment->setIsAbused(1);
    $comment->save();
    
    $this->getResponse()->setContent("Abused Successfully!");
    $this->setLayout(false);
    return sfView::NONE;
      
  }
}
